import { _decorator, Component, EventTouch, Input, input, Node, math } from 'cc';
import { GameManager } from '../framework/GameManager';
const { ccclass, property } = _decorator;

@ccclass('UIMain')
export class UIMain extends Component {
    @property
    private planeSpeed = 1;

    @property(Node)
    private selfPlane: Node;

    @property(GameManager)
    private gameManager: GameManager;

    @property(Node)
    private gameStart: Node = null;

    @property(Node)
    private game: Node = null;

    @property(Node)
    private gameOver: Node = null;


    private delta = new math.Vec2();

    start() {
        this.node.on(Input.EventType.TOUCH_MOVE, this.touchMove, this)
        this.node.on(Input.EventType.TOUCH_START, this.touchStart, this)
        this.node.on(Input.EventType.TOUCH_END, this.touchEnd, this)

        this.gameStart.active = true; 
    }

    public  restart(){
        this.gameOver.active=false;
        this.game.active=true;
        this.gameManager.playAudioEffect("button");
        this.gameManager.restart();
    }

    public returnMain(){
         this.gameOver.active=false;
         this.gameStart.active=true;
         this.gameManager.playAudioEffect("button");
         this.gameManager.returnMain();
    }

    touchMove(event: EventTouch) {
        if (this.gameManager.isgameStart) {
            event.getDelta(this.delta);

            let pos = this.selfPlane.position;
            this.selfPlane.setPosition(pos.x + 0.01 * this.planeSpeed * this.delta.x, pos.y, pos.z - 0.01 * this.planeSpeed * this.delta.y)
        }
    }

    touchStart(event: EventTouch) {

        if (this.gameManager.isgameStart) {
            this.gameManager.isShooting(true);
        } else {
            this.gameStart.active = false;
            this.game.active = true;
            this.gameManager.playAudioEffect("button");
            this.gameManager.gameStart();
        }
    }
    touchEnd(event: EventTouch) {

        if (!this.gameManager.isgameStart) {
            return;
        }
        this.gameManager.isShooting(false);
    }

    update(deltaTime: number) {

    }
}

